//
//  RankingViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 2016/10/25.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "RankingViewController.h"
#import "YJSegmentedControl.h"
#import "UIImage+ImageEffects.h"
#import "CLUIBaseTableViewController+Empty.h"

typedef NS_ENUM(NSInteger, RankingType) {
    RankingTypeDay = 1,     //日排行
    RankingTypeWeek,        //周排行

};

@interface RankingViewController ()<YJSegmentedControlDelegate>
@property (strong, nonatomic) NSMutableArray *arrDataSource;
@property (strong, nonatomic) NSMutableArray *arrDataSourceWeek;
@property (strong, nonatomic) NSMutableDictionary *dictModel;
@property (strong, nonatomic) NSMutableDictionary *dictModelWeek;
@property (assign, nonatomic) NSInteger page;
@property (assign, nonatomic) RankingType rankingType;
@end

@implementation RankingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"健康排行";
    [self showNavgationWithLeftItemWithTitle:@"首页"];
    NSArray * btnDataSource = @[@"日排行榜", @"周排行榜"];
    YJSegmentedControl * segment = [YJSegmentedControl segmentedControlFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44) titleDataSource:btnDataSource backgroundColor:[UIColor whiteColor] titleColor:[UIColor grayColor] titleFont:[UIFont fontWithName:@".Helvetica Neue Interface" size:16.0f] selectColor:RGB(82, 177, 219) buttonDownColor:RGB(82, 177, 219) Delegate:self];
    self.tableView.tableHeaderView = segment;
    self.rankingType = RankingTypeDay;
    [self getRankingList];
}

- (void)getRankingList{
    [SVProgressHUD showWithStatus:@"获取排行列表.." maskType:SVProgressHUDMaskTypeClear];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:[[NSUserDefaults standardUserDefaults] objectForKey:UserDataID] forKey:@"user_id"];
    [dict setObject:[NSNumber numberWithInt:self.rankingType] forKey:@"type"];
    WS(weakSelf);
    [[BaseDataRequest alloc] getRankingList:dict withBlock:^(id responseObject, NSString *status, NSError *error) {
        [SVProgressHUD dismiss];
        if ([status isEqualToString:@"0"]) {
            if(self.rankingType == RankingTypeDay){
                self.dictModel = [responseObject objectForKey:@"result"];
                [self.arrDataSource addObjectsFromArray:[self.dictModel objectForKey:@"list"]];
                if(self.arrDataSource.count == 0){
                    [weakSelf showLabelEmptyCate:CGRectMake((SCREEN_WIDTH - 100)/2, (SCREEN_HEIGHT -140 -64)/2, 100, 100) withTitle:@"这里还没有内容" withImage:[UIImage imageNamed:@"icon_ blank"]];
                    self.tableView.footer.hidden = YES;
                }
            }else{
                self.dictModelWeek = [responseObject objectForKey:@"result"];
                [self.arrDataSourceWeek addObjectsFromArray:[self.dictModelWeek objectForKey:@"list"]];
                if(self.arrDataSourceWeek.count == 0){
                    [weakSelf showLabelEmptyCate:CGRectMake((SCREEN_WIDTH - 100)/2, (SCREEN_HEIGHT -140 -64)/2, 100, 100) withTitle:@"这里还没有内容" withImage:[UIImage imageNamed:@"icon_ blank"]];
                    self.tableView.footer.hidden = YES;
                }
            }
            [self.tableView reloadData];
        }else{
            if (error != nil) {
                if(self.arrDataSource.count == 0 ||self.dictModelWeek.count == 0){
                    [weakSelf showLabelEmptyCate:CGRectMake((SCREEN_WIDTH - 100)/2, (SCREEN_HEIGHT -140 -64)/2, 100, 100) withTitle:@"出错了，稍后再试吧" withImage:[UIImage imageNamed:@"icon_wifi"]];
                    self.tableView.footer.hidden = YES;
                }else{
                    TIPSFORVIEW(@"请检测网络设置");
                }
            }else{
                TIPSFORVIEW([responseObject objectForKey:@"message"]);
            }
        }
    }];
}

#pragma  mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(self.rankingType == RankingTypeDay){
        return _arrDataSource.count;
    }else{
        return _arrDataSourceWeek.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier = @"RankingCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
    }
    UIImageView *imageV = [cell viewWithTag:1];
    UILabel *labRanking = [cell viewWithTag:2];
    UIImageView *imageUser = [cell viewWithTag:3];
    UILabel *labUserName = [cell viewWithTag:4];
    UILabel *labStepCount = [cell viewWithTag:5];
    NSMutableDictionary *dicr = [NSMutableDictionary dictionary];
    if(self.rankingType == RankingTypeDay){
        dicr = _arrDataSource[indexPath.row];
    }else{
        dicr = _arrDataSourceWeek[indexPath.row];
    }
    if (indexPath.row == 0) {
        imageV.hidden = YES;
        labRanking.hidden = NO;
        labRanking.text = [dicr objectForKey:@"rowNo"];
        labUserName.text = @"我";
        if([[dicr objectForKey:@"rowNo"] isEqualToString:@"-1"]){
            labStepCount.text = @"未佩戴";
            labRanking.hidden = YES;
        }else{
            labRanking.hidden = NO;
            labStepCount.text = [NSString stringWithFormat:@"%@步",[dicr objectForKey:@"activity"]];
        }
    }else{
        if (indexPath.row < 4) {
            imageV.hidden = NO;
            imageV.image = [UIImage imageNamed:[NSString stringWithFormat:@"no_%d",(int)indexPath.row]];
            labRanking.hidden = YES;
        }else{
            imageV.hidden = YES;
            labRanking.hidden = NO;
            labRanking.text = [dicr objectForKey:@"rowNo"];
        }
        labUserName.text = [dicr objectForKey:@"name"];
        labStepCount.text = [NSString stringWithFormat:@"%@步",[dicr objectForKey:@"activity"]];
    }
    [imageUser sd_setImageWithURL:[NSURL URLWithString:[dicr objectForKey:@"picpath"]] placeholderImage:[UIImage imageNamed:@"icon_user-1"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if(image == nil){
            imageUser.image = [UIImage imageNamed:@"icon_user-1"];
        }else{
            imageUser.image = [image  circleImage];
        }
    }];
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];//创建一个视图
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, SCREEN_WIDTH, 20)];
    headerLabel.font = [UIFont systemFontOfSize:15.0];
    headerLabel.textColor = [UIColor grayColor];
    headerLabel.textAlignment = NSTextAlignmentCenter;
    if(self.rankingType == RankingTypeDay){
        NSString *title = [self.dictModel objectForKey:@"time"];
        if (title.length == 0 || title == nil) {
            headerLabel.text = @"";
        }else{
            headerLabel.text = title;
        }
    }else{
        NSString *title = [self.dictModelWeek objectForKey:@"time"];
        if (title.length == 0 || title == nil) {
            headerLabel.text = @"";
        }else{
            headerLabel.text = title;
        }
    }
    [headerView addSubview:headerLabel];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

#pragma mark - YJSegmentedControlDelegate
- (void)segumentSelectionChange:(NSInteger)selection{
     [self clearLabelEmptyCate];
    if (selection == 0) {
        self.rankingType = RankingTypeDay;
        if (self.arrDataSource.count == 0) {
            [self getRankingList];
        }
    }else if (selection == 1){
        self.rankingType = RankingTypeWeek;
        if (self.arrDataSourceWeek.count == 0 ) {
            [self getRankingList];
        }
    }
    [self.tableView reloadData];
}

- (NSMutableArray *)arrDataSource{
    if (_arrDataSource == nil) {
        _arrDataSource = [NSMutableArray array];
    }
    return _arrDataSource;
}

- (NSMutableArray *)arrDataSourceWeek{
    if (_arrDataSourceWeek == nil) {
        _arrDataSourceWeek = [NSMutableArray array];
    }
    return _arrDataSourceWeek;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
